package www.xudan.android_ffmpeg.videoplayer.sample;

import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import www.xudan.android_ffmpeg.Constance;
import www.xudan.android_ffmpeg.R;
import www.xudan.android_ffmpeg.videoplayer.PlayerManager;
import www.xudan.android_ffmpeg.videoplayer.model.VideoModel;
import www.xudan.android_ffmpeg.videoplayer.widget.ListItemPlayerView;

import static android.widget.AbsListView.OnScrollListener.SCROLL_STATE_IDLE;
import static androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_DRAGGING;
import static androidx.viewpager.widget.ViewPager.SCROLL_STATE_SETTLING;


public class SampleActivity extends AppCompatActivity {

    private RecyclerView rv;
    private LinearLayoutManager linearLayoutManager;
    private int firstVisibleItem;
    private int lastVisibleItem;
    private int visibleCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.player_sample_main);
        rv = findViewById(R.id.rv);
        List<VideoModel> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add(new VideoModel(Constance.http_path, "标清"));
        }
        linearLayoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(linearLayoutManager);
        rv.setAdapter(new MyAdapter(this, list));
        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case SCROLL_STATE_IDLE: //滚动停止
                        if (PlayerManager.getInstance().getPlayingView() != null) {
                            PlayerManager.getInstance().getPlayingView().onEndScrolled();
                        }
                        break;
                    case SCROLL_STATE_DRAGGING: //手指拖动
                        if (PlayerManager.getInstance().getPlayingView() != null) {
                            PlayerManager.getInstance().getPlayingView().onStartScrolled();
                        }
                        break;
                    case SCROLL_STATE_SETTLING: //惯性滚动
                        if (PlayerManager.getInstance().getPlayingView() != null) {
                            PlayerManager.getInstance().getPlayingView().onStartScrolled();
                        }
                        break;
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                visibleCount = lastVisibleItem - firstVisibleItem;
                if (PlayerManager.getInstance().getPlayingView() != null
                        && (((ListItemPlayerView) PlayerManager.getInstance().getPlayingView()).getItemIndex() > lastVisibleItem
                        || ((ListItemPlayerView) PlayerManager.getInstance().getPlayingView()).getItemIndex() < firstVisibleItem)) {
                    //有播放器划出了可见范围
                    ((ListItemPlayerView) PlayerManager.getInstance().getPlayingView()).onItemRollOut();
                }
            }
        });
    }
}
